package com.syt.cmn.controller;


import com.syt.cmn.service.DictService;
import com.syt.model.cmn.Dict;
import com.syt.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author xxxx
 * @since 2023-02-05
 */
@Api(tags = "数据字典")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin 有 gateway 做了跨域
public class DictController {

    @Autowired
    private DictService dictService;


    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
          List<Dict> list= dictService.findByDictCode(dictCode);
          return Result.ok(list);
    }


    @ApiOperation(value = "根据value查询")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value) {
        System.out.println("value=" + value);
        String dictName = dictService.getDictName("", value);
        return dictName;
    }

    @ApiOperation(value = "根据dictCode和value查询")
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }


    @ApiOperation(value = "导入字典数据")
    @PostMapping("/importDictData")
    public void importDictData(MultipartFile file) {
        dictService.importDictData(file);
    }


    @ApiOperation(value = "导出数据字典接口")
    @GetMapping(value = "/exportDictData")
    public void exportDictData(HttpServletResponse response) {
        dictService.exportData(response);
    }


    @ApiOperation(value = "根据数据字典id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

}
